﻿package mortal.game.scene3D.fight
{
    import Message.DB.Tables.*;
    import Message.Public.*;
    import baseEngine.modifiers.*;
    import com.gengine.debug.*;
    import com.gengine.utils.*;
    import flash.geom.*;
    import frEngine.event.*;
    import mortal.component.gconst.*;
    import mortal.game.model.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.scene3D.events.*;
    import mortal.game.scene3D.layer3D.utils.*;
    import mortal.game.scene3D.model.data.*;
    import mortal.game.scene3D.model.player.*;
    import mortal.game.scene3D.player.entity.*;
    import mortal.game.scene3D.player.info.*;
    import mortal.game.utils.*;
    import mortal.game.view.skill.*;

    public class FightEffectNormal extends FightEffectBase
    {
        protected var skillModel:TSkillModel;
        protected var direction:Number = 0;
        protected var isHasFromPlayer:Boolean = false;
        private var reachHitNumber:int = 0;

        public function FightEffectNormal()
        {
            return;
        }// end function

        override public function runStart() : void
        {
            var _loc_3:EntityInfo = null;
            var _loc_4:EntityModelInfo = null;
            var _loc_5:Boolean = false;
            super.runStart();
            this.isHasFromPlayer = _fromPlayer && _fromPlayer.parent;
            if (_skill.skillId == GameConst.PetNormalSkillId && _fromPlayer && _fromPlayer is PetPlayer && _fromPlayer.entityInfo)
            {
                _loc_3 = _fromPlayer.entityInfo;
                if (_loc_3)
                {
                    _loc_4 = PetUtil.getPetModel(1, _loc_3.petModelId);
                    if (_loc_4.skillEffect)
                    {
                        this.skillModel = SkillModelConfig.instance.getInfoById(_loc_4.skillEffect);
                    }
                }
            }
            else
            {
                this.skillModel = SkillModelConfig.instance.getInfoById(_skill.skillModel);
            }
            if (this.isHasFromPlayer)
            {
                if (this.skillModel && this.skillModel.isMultiple || _fromPlayer is MonsterPlayer && (_fromPlayer as MonsterPlayer).bossInfo.aiBehavior & 8)
                {
                }
                else if (_targetPoint)
                {
                    this.direction = MathUitl.getAngleByXY(_fromPlayer.x2d, _fromPlayer.y2d, _targetPoint.x, _targetPoint.y);
                    _fromPlayer.direction = this.direction;
                }
                else if (_targetPlayer && _targetPlayer != _fromPlayer)
                {
                    this.direction = MathUitl.getAngleByXY(_fromPlayer.x2d, _fromPlayer.y2d, _targetPlayer.x2d, _targetPlayer.y2d);
                    _fromPlayer.direction = this.direction;
                }
            }
            var _loc_1:Boolean = false;
            if (!this.skillModel)
            {
                runFinish();
                return;
            }
            if (this.skillModel.isTargetModelImm)
            {
                this.playTargetEffect();
            }
            this.playWarnEffect();
            var _loc_2:Number = 0;
            if (this.skillModel.action)
            {
                if (this.isHasFromPlayer)
                {
                    _fromPlayer.attack(this.skillModel.action);
                }
                if (!SkillUtil.isBrillyGouriSkill(_skill.skillId))
                {
                    if (_skill.type == ESkillType._ESkillTypeRushForward && _skill.useTime > 0)
                    {
                        runFinish();
                        return;
                    }
                }
                if (this.isHasFromPlayer)
                {
                    if (this.skillModel.daoguangModel && _isNotShildEffect)
                    {
                        SkillEffectUtil.addPlayerEffect(_fromPlayer, this.skillModel.daoguangModel, true, _isFromRolePlayer, null, PlayMode.ANIMATION_STOP_MODE, true);
                    }
                    if (this.skillModel.selfModel && _isNotShildEffect)
                    {
                        if (this.skillModel.notSelfMove)
                        {
                            SkillEffectUtil.addPointEffect(new Point(_fromPlayer.x2d, _fromPlayer.y2d), this.skillModel.selfModel, _isFromRolePlayer, this.direction, layer);
                        }
                        else
                        {
                            if (_skill && _skill.type == ESkillType._ESkillTypeRushForward)
                            {
                                _loc_2 = this.direction;
                                _loc_2 = _loc_2 + 0.01;
                            }
                            SkillEffectUtil.addPlayerEffect(_fromPlayer, this.skillModel.selfModel, true, _isFromRolePlayer, null, PlayMode.ANIMATION_STOP_MODE, false, false, _loc_2);
                        }
                    }
                }
                if (!this.isHasFromPlayer)
                {
                    this.reachTargetHandler();
                    return;
                }
                if (ActionType.isAttackAction(_fromPlayer.bodyPlayer.actionName))
                {
                    FrEventDispatcher.instance.proxyAddEventListener(_fromPlayer, PlayerEvent.PLAYER_FIRE, this.addChainEffect);
                    if (this.skillModel.trackModel)
                    {
                        FrEventDispatcher.instance.proxyAddEventListener(_fromPlayer, PlayerEvent.PLAYER_FIRE, this.trackHandler);
                    }
                    else
                    {
                        FrEventDispatcher.instance.proxyAddEventListener(_fromPlayer, PlayerEvent.PLAYER_FIRE, this.reachTargetHandler);
                    }
                }
                else
                {
                    this.addChainEffect();
                    if (this.skillModel.trackModel)
                    {
                        this.trackHandler();
                    }
                    else
                    {
                        this.reachTargetHandler();
                    }
                }
            }
            else
            {
                _loc_5 = false;
                if (this.isHasFromPlayer)
                {
                    if (this.skillModel.daoguangModel && _isNotShildEffect)
                    {
                        SkillEffectUtil.addPlayerEffect(_fromPlayer, this.skillModel.daoguangModel, true, _isFromRolePlayer, null, PlayMode.ANIMATION_STOP_MODE, true);
                    }
                    if (this.skillModel.selfModel && _isNotShildEffect)
                    {
                        if (this.skillModel.notSelfMove)
                        {
                            SkillEffectUtil.addPointEffect(new Point(_fromPlayer.x2d, _fromPlayer.y2d), this.skillModel.selfModel, _isFromRolePlayer, this.direction, layer);
                        }
                        else
                        {
                            if (_skill && _skill.type == ESkillType._ESkillTypeRushForward)
                            {
                                _loc_2 = this.direction;
                                _loc_2 = _loc_2 + 0.01;
                            }
                            SkillEffectUtil.addPlayerEffect(_fromPlayer, this.skillModel.selfModel, true, _isFromRolePlayer, null, PlayMode.ANIMATION_STOP_MODE, false, false, _loc_2);
                        }
                    }
                }
                if (this.skillModel.trackModel)
                {
                    _loc_5 = true;
                    this.trackHandler();
                }
                else
                {
                    this.reachTargetHandler();
                }
                if (!_loc_5)
                {
                    runFinish();
                }
                return;
            }
            return;
        }// end function

        protected function playWarnEffect() : void
        {
            var _loc_2:Point = null;
            var _loc_3:Number = NaN;
            var _loc_4:Number = NaN;
            var _loc_5:Number = NaN;
            var _loc_1:Boolean = false;
            if (_fromPlayer && !(_fromPlayer is RolePlayer))
            {
                if (_fromPlayer.entityInfo && EntityRelationUtil.isFriend(_fromPlayer.entityInfo.entityInfo))
                {
                    _loc_1 = true;
                }
            }
            if (this.skillModel.warnAreaModel && _isNotShildEffect && !_loc_1)
            {
                if (_targetPoint && _skill.type == ESkillType._ESKillTypeJumpCut && _fromPlayer)
                {
                    _loc_2 = new Point();
                    _loc_3 = MathUitl.getDistance(_targetPoint.x, _fromPlayer.x2d, _targetPoint.y, _fromPlayer.y2d);
                    _loc_4 = MathUitl.getRadiansByXY(_fromPlayer.x2d, _fromPlayer.y2d, _targetPoint.x, _targetPoint.y);
                    _loc_2.x = _targetPoint.x + Math.cos(_loc_4) * GameConst.JumpCutWeaponDis;
                    _loc_2.y = _targetPoint.y + Math.sin(_loc_4) * GameConst.JumpCutWeaponDis;
                }
                else if (_targetPoint)
                {
                    _loc_2 = new Point();
                    _loc_2.x = _targetPoint.x;
                    _loc_2.y = _targetPoint.y;
                }
                else if (!_targetPoint && _targetPlayer)
                {
                    _loc_2 = new Point();
                    _loc_2.x = _targetPlayer.x2d;
                    _loc_2.y = _targetPlayer.y2d;
                }
                if (_loc_2 && this.skillModel.warnAreaModel)
                {
                    _loc_5 = 0;
                    Log.debugMsg(LogType.other, _loc_2.x, _loc_2.y);
                    SkillEffectUtil.addPointEffect(_loc_2, this.skillModel.warnAreaModel, _isFromRolePlayer, _loc_5, layer);
                }
            }
            return;
        }// end function

        protected function playTargetEffect() : void
        {
            var _loc_1:Number = NaN;
            if (this.skillModel.targetModel)
            {
                if (!_targetPoint && _targetPlayer)
                {
                    _targetPoint = new Point(_targetPlayer.x2d, _targetPlayer.y2d);
                }
                if (_targetPoint && _isNotShildEffect)
                {
                    _loc_1 = 0;
                    if (this.skillModel.isTargetDirection)
                    {
                        _loc_1 = this.direction;
                    }
                    SkillEffectUtil.addPointEffect(_targetPoint, this.skillModel.targetModel, _isFromRolePlayer, _loc_1, layer);
                }
            }
            return;
        }// end function

        protected function addChainEffect(param1 = null) : void
        {
            var _loc_2:SpritePlayer = null;
            if (this.skillModel.chainEffect && _fromPlayer && _targetPlayer)
            {
                if (this.skillModel.isMultiple)
                {
                    for each (_loc_2 in _hitPlayers)
                    {
                        
                        FightChainUtil.addChain(_fromPlayer, _loc_2, this.skillModel.chainEffect, 1500);
                    }
                }
                else
                {
                    FightChainUtil.addChain(_fromPlayer, _targetPlayer, this.skillModel.chainEffect, 1500);
                }
            }
            return;
        }// end function

        protected function trackHandler(param1 = null) : void
        {
            var _loc_2:SpritePlayer = null;
            var _loc_3:* = undefined;
            if (this.skillModel.isMultiple)
            {
                for each (_loc_2 in _hitPlayers)
                {
                    
                    if (_fromPlayer && _loc_2)
                    {
                        this.track(_fromPlayer, _loc_2, this.skillModel.trackModel, this.reachTargetHandler);
                    }
                }
            }
            else if (_fromPlayer)
            {
                if (_targetPlayer)
                {
                    if (this.skillModel.targetModel)
                    {
                        _loc_3 = new Point(_targetPlayer.x2d, _targetPlayer.y2d);
                    }
                    else
                    {
                        _loc_3 = _targetPlayer;
                    }
                }
                else if (_targetPoint)
                {
                    _loc_3 = _targetPoint;
                }
                if (_loc_3)
                {
                    this.track(_fromPlayer, _loc_3, this.skillModel.trackModel, this.reachTargetHandler);
                }
            }
            return;
        }// end function

        private function track(param1:SpritePlayer, param2, param3:String, param4:Function) : void
        {
            var target:*;
            var trackReachHandler:Function;
            var effectPlayerTrack:EffectPlayer;
            var fromPlayer:* = param1;
            var obj:* = param2;
            var url:* = param3;
            var callBack:* = param4;
            trackReachHandler = function () : void
            {
                callBack.call(null, obj);
                return;
            }// end function
            ;
            if (obj is SpritePlayer)
            {
                target = obj;
            }
            else if (obj is Point)
            {
                target = Scene3DUtil.changeMap2dToScene3d((obj as Point).x, (obj as Point).y);
            }
            else
            {
                callBack.call(null, obj);
                return;
            }
            if (_isNotShildEffect)
            {
                effectPlayerTrack = SkillEffectUtil.addTrackPlayer(fromPlayer, target, url, trackReachHandler);
            }
            else
            {
                callBack.call(null, obj);
            }
            return;
        }// end function

        protected function reachTargetHandler(param1 = null) : void
        {
            var _loc_2:SpritePlayer = null;
            var _loc_3:Number = NaN;
            if (_isFinish)
            {
                return;
            }
            if (param1 is SpritePlayer)
            {
                _loc_2 = param1 as SpritePlayer;
            }
            if (this.skillModel.isMultiple)
            {
                var _loc_4:String = this;
                var _loc_5:* = this.reachHitNumber + 1;
                _loc_4.reachHitNumber = _loc_5;
                if (this.reachHitNumber == _hitPlayers.length)
                {
                    runFinish();
                }
            }
            else
            {
                if (!this.skillModel.isTargetModelImm)
                {
                    this.playTargetEffect();
                }
                runFinish();
            }
            return;
        }// end function

        protected function removeListeners() : void
        {
            FrEventDispatcher.instance.proxyRemoveEventListener(_fromPlayer, PlayerEvent.PLAYER_FIRE, this.reachTargetHandler);
            FrEventDispatcher.instance.proxyRemoveEventListener(_fromPlayer, PlayerEvent.PLAYER_FIRE, this.trackHandler);
            FrEventDispatcher.instance.proxyRemoveEventListener(_fromPlayer, PlayerEvent.PLAYER_FIRE, this.addChainEffect);
            return;
        }// end function

        override public function dispose() : void
        {
            this.removeListeners();
            super.dispose();
            this.isHasFromPlayer = false;
            this.skillModel = null;
            this.direction = 0;
            this.reachHitNumber = 0;
            return;
        }// end function

    }
}
